package com.itheima.ydd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import java.util.*;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
@Slf4j
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    /**
     * 获取list中的map
     *
     * @param key
     * @param name 可变参数--数组
     * @return
     */
    @Override
    public Map<String, Object> getListMap(String key, String... name) {
        Map<String, Object> listMap = new HashMap<>();
        listMap.put("name", name[0]);
        listMap.put("key", key);
        //children:List<Map<String,Object>>
        List<Map<String, Object>> children = new ArrayList<>();
        //childrenMap
        for (int i = 0; i < name.length - 1; i++) {
            Map<String, Object> childrenMap = new HashMap<>();
            String childrenName = name[i + 1];
            childrenMap.put("name", childrenName);

            LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysConfig::getTabValue, key);
            wrapper.eq(SysConfig::getGroupValue, childrenName);
            List<SysConfig> childrenData = this.list(wrapper);

            childrenMap.put("data", childrenData);
            children.add(childrenMap);
        }

        listMap.put("children", children);

        return listMap;

    }

    /**
     * 获取系统明细中的list
     * @return
     */
    @Override
    public List<Map<String, Object>> getList() {
        //设置list
        List<Map<String, Object>> list = new ArrayList<>();
        //list下面3个map,每个map封装name,key,children
        //map1:基础配置
        Map<String, Object> listMap4 = getListMap("base", "基础配置", "站点配置", "底部信息", "其他配置");
        List<Map<String, Object>> children = (List<Map<String, Object>>) listMap4.get("children");
        List<Map<String, Object>> sysConfig125 = get125ConfigValue();

        lo:
        for (Map<String, Object> child : children) {
            List<SysConfig> dataList = (List<SysConfig>) child.get("data");
            for (SysConfig sysConfig : dataList) {
                if (sysConfig.getId() == 125) {
                    sysConfig.setConfigValue(sysConfig125.toString());
                    break lo;
                }
            }
        }

        //map2:系统设置
        Map<String, Object> listMap2 = getListMap("system", "系统设置", "管理系统配置");

        //map3:系统设置
        Map<String, Object> listMap3 = getListMap("custom", "其他设置", "通知公告", "前台图片");

        list.add(listMap4);
        list.add(listMap2);
        list.add(listMap3);

        return list;

    }

    /**
     * 给id为125的sys_config的ConfigValue赋值
     * @return
     */
    @Override
    public List<Map<String, Object>> get125ConfigValue() {
        List<Map<String, Object>> sysConfig125 = new ArrayList<>();
        Map<String, Object> sysConfig125Map1 = new HashMap<>();
        sysConfig125Map1.put("label", "是");
        sysConfig125Map1.put("value", "1");
        Map<String, Object> sysConfig125Map2 = new HashMap<>();
        sysConfig125Map2.put("label", "否");
        sysConfig125Map2.put("value", "2");
        sysConfig125.add(sysConfig125Map1);
        sysConfig125.add(sysConfig125Map2);
        return sysConfig125;
    }

}
